<?php 
class ArrowCtrlDatasource extends ArrowTagContainer{
	
	public $criteria;
	
	public function configure(){
		$this->addProperties(array(
			"limit" => false,
			"key" => null,
			"groupBy" => false,
			"orderBy" => false,
		));
		$this->setDisplay(false);
	}
	
	public function getData( $dataSet = false ){
		
		if($query = $this->getFirstChild("ArrowCtrlQuery"))
			return ArrowOrmSqlRouter::toArray(ArrowOrmSqlRouter::query($query->getQuery()));
		
		
		$key = $this->getProperty("key");
		$criteria = $this->getCriteria();
		$model = $criteria->getModel();
		ArrowController::importModel($model);
		$class = ArrowModel::getModelClass($model);
		
		if($key !== null){
			if($key === false)
				return false;
			return call_user_func(array($class, "getByKey"), $key,$class);
		}

		
		if($dataSet){
			return call_user_func(array($class, "getDataSetByCriteria"), $criteria, $class);
		}
		
		return call_user_func(array($class, "getByCriteria"), $criteria,$class);
	}
	
	/**
	 * Returns criteria
	 * @return ArrowOrmCriteria
	 */
	public function init(){
		
		if($this->getFirstChild("ArrowCtrlQuery"))
			return true;
			
			
			
		$criteria = new Criteria(); 
		
		$models = $this->getChildren("ArrowCtrlModel");
		if(empty($models))
			throw new ArrowException("[ArrowCtrlCriteria] No model specyfied");
		if(count($models)==1){
			$criteria = new Criteria($models[0]->getModel());
		}else{
			$tmp = array();
			foreach($models as $model) $tmp[] = $model->getModel();
			$criteria = new ArrowOrmJoinCriteria($tmp);
		}
		
		
		$joins = $this->getChildren("ArrowCtrlJoin");
		foreach($joins as $join){
			$criteria->addVirtualRelation($join->getProperty("baseClass"), $join->getProperty("baseField"), $join->getProperty("targetClass"), $join->getProperty("targetField"));
		}
		
		
		$conditions = $this->getChildren("ArrowCtrlCondition");
		foreach($conditions as $condition){
			$criteria->addCondition( $condition->getField(), $condition->getValue(), $condition->getType() );
		}
		
		
		$functions = $this->getChildren("ArrowCtrlFunction");
		foreach($functions as $function){
			call_user_func_array(array($criteria, $function->getName()), $function->getArguments());
		}
		
		
		$limit = $this->getProperty("limit");
		if($limit != false){
			$tmp = explode(";", $limit);
			$criteria->setLimit($tmp[0], $tmp[1]);
		}
		
		
		
		
		$groupBy = $this->getProperty("groupBy");
		if($groupBy != false){
			$expl = explode(";", $groupBy);
			foreach($expl as $field)
				$criteria->addGroupBy( $field );
		}
		
		$orderBy = $this->getProperty("orderBy");
		if($orderBy != false){
			$fields = explode(';', $orderBy);
			
			foreach($fields as $field) {
				
				$tmp = explode(' ', $field);
				if(!isset($tmp[1]))
					$tmp[1] = 'asc';
				
				$criteria->addOrderBy( $tmp[0], $tmp[1] );
			}
		}
		
		$columns = $this->getChildren("ArrowCtrlColumn");
		if(!empty($columns)){
			$criteria->setEmptyList();
			foreach($columns as $column){
				$criteria->addColumn($column->getName(), $column->getProperty("alias"), $column->getProperty("agregate"));
			}
		}
		
		$this->criteria = $criteria;
	}
	
	public function getCriteria(){
		return $this->criteria;
	}
	
	public function setCriteria( $criteria ){
		$this->criteria = $criteria;
		return $this->setProperty("criteria", $criteria);
	}
}
?>